package servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import slope.CalculateGradient;
import slope.GradientItem;

public class GradientServlet extends HttpServlet {
	private static int larger45Count = 0;

	/**
	 * Constructor of the object.
	 */
	public GradientServlet() {
		super();
	}

	/**
	 * Destruction of the servlet. <br>
	 */
	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}

	/**
	 * The doGet method of the servlet. <br>
	 * 
	 * This method is called when a form has its tag value method equals to get.
	 * 
	 * @param request
	 *            the request send by the client to the server
	 * @param response
	 *            the response send by the server to the client
	 * @throws ServletException
	 *             if an error occurred
	 * @throws IOException
	 *             if an error occurred
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String str_id = request.getParameter("id");
		int id = Integer.parseInt(str_id);
		CalculateGradient cg = new CalculateGradient();
		List<GradientItem> items = cg.getGradientItemsById(id);
		JSONObject jsonItem = null;
		JSONArray array = new JSONArray();
		for (GradientItem item : items) {
			jsonItem = new JSONObject();
			jsonItem.put("id", item.getDeciceId());
			jsonItem.put("time", item.getTime());
			jsonItem.put("angle", item.getAngle() + "");
			array.add(jsonItem);
			if(item.getAngle()>=45){
				larger45Count++;
			}
			
		}
		JSONObject rows = new JSONObject();
		rows.put("rows", array.toString());
		response.setContentType("text/html");
		response.setCharacterEncoding("utf-8");
		PrintWriter out = response.getWriter();

		out.write(rows.toString());
		out.flush();
		out.close();
		System.out.println(rows.toString());
	}

	public static int getLarger45Count() {
		return larger45Count;
	}

	/**
	 * The doPost method of the servlet. <br>
	 * 
	 * This method is called when a form has its tag value method equals to
	 * post.
	 * 
	 * @param request
	 *            the request send by the client to the server
	 * @param response
	 *            the response send by the server to the client
	 * @throws ServletException
	 *             if an error occurred
	 * @throws IOException
	 *             if an error occurred
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doGet(request, response);
	}

	/**
	 * Initialization of the servlet. <br>
	 * 
	 * @throws ServletException
	 *             if an error occurs
	 */
	public void init() throws ServletException {
		// Put your code here
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		int id = 1;
		CalculateGradient cg = new CalculateGradient();
		List<GradientItem> items = cg.getGradientItemsById(id);
		JSONObject jsonItem = null;
		JSONArray array = new JSONArray();
		for (GradientItem item : items) {
			jsonItem = new JSONObject();
			jsonItem.put("device_id", item.getDeciceId());
			jsonItem.put("time", item.getTime());
			jsonItem.put("angle", item.getAngle());
			jsonItem.put("content", item.getContent());
			array.add(jsonItem);
		}
		// String content = xmlConvert.getFusionChartsXML(startDate, endDate,
		// id);
		System.out.println(array.toString());
	}
}
